layui.define(['jquery','table', 'form','custom'], function(exports) {
    "use strict";
    var $ = layui.$, larryms = layui.larryms, form = layui.form , layer = layui.layer, beone = layui.custom;
    table = layui.table;
    //按钮组监听
    $('.larryms-btn-group .layui-btn').on('click', function() {
        var type = $(this).data('type'),
            url = $(this).data('url'),
            tit = $(this).data('title');
        active[type] ? active[type].call(this, url, tit) : '';
    });

    $("#searchBtn,#reloadBtn").on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    var pageTableID = $('.larryms-table-id').attr('id');

    var active = {
        query: function () {
            var formId = $(this).data("form");
            table.reload(pageTableID, {
                where: beone.serializeObject(formId)
            });
        },
        reload: function () {
            var formId = $(this).data("form");
            table.reload(pageTableID, {
                where: beone.clearForm(formId)
            });
        },
        add: function (url , title) {
            var that = this;
            if ($(that).attr('data-flag') == 'exist') {
                var index = layui.cache.layerIndex,
                $max = $('#layui-layer' + index).find('.layui-layer-maxmin');
                $max.click();
                layui.cache.layerIndex = null;
            } else {
                $(that).attr('data-flag', 'exist');
                var index = beone.dialog({
                    title: title, url: url, width: 700, height: 500,
                    success: function (index, layero) {
                        form.render();
                    }, end: function () {
                        $(that).removeAttr("data-flag");
                        layui.cache.layerIndex = null;
                    }
                });
            }
        },
        update: function (url , title) {
            var checkStatus = table.checkStatus(pageTableID)
                , data = checkStatus.data;
            if (data.length != 1) {
                layer.msg('请选择一行编辑', {icon: 5});
                return false;
            }
            showUpdate(data[0].userId, url, title,true);
        },
        detail: function (url , title) {
            var checkStatus = table.checkStatus(pageTableID)
                , data = checkStatus.data;
            if (data.length != 1) {
                layer.msg('请选择一行查看', {icon: 5});
                return false;
            }
            showUpdate(data[0].userId,  url, title,false);
        }
    };

    //监听工具条
    table.on('tool(' + pageTableID + ')', function (obj) {
        var data = obj.data;
        var url = $(this).data('url');
        var title = $(this).data('title');
        if (obj.event === 'detail') {
            showUpdate(data.userId,url,title,false);
        } else if (obj.event === 'del') {
            var index = beone.confirm('确定删除用户[<label style="color: #00AA91;">' + data.userAccount + '</label>]?',function () {
                beone.doRequest(url, {userId:data.userId}, function () {
                    layer.close(index);
                    table.reload(pageTableID);
                });
            })
        } else if (obj.event === 'edit') {
            showUpdate(data.userId, url,title, true);
        }else if (obj.event === 'resetPwd') {
            var index = beone.confirm('确定重置账户[<label style="color: #00AA91;">' + data.userAccount
                + '</label>]的密码?',function () {
                beone.doRequest(url, {userId:data.userId}, function () {
                    layer.close(index);
                });
            })
        }
    });
    function showUpdate(id, url,title,flag) {
        url +=  id + "/showUpdate" + (flag ? "" : "?detail=true");
        beone.dialog({title:title, url:url, width:700, height:500});
    }

    var dataUrl = $('#' + pageTableID).data('url')
    //方法级渲染
    table.render({
        id: pageTableID, elem: '#' + pageTableID,method:'POST' , url: dataUrl
        , cols: [[ {checkbox: true, fixed: true, width: '3%'}
            , {field: 'userAccount', title: '账户', width: '8%'}
            , {field: 'userName', title: '姓名', width: '8%'}
            , {field: 'userMobile', title: '手机', width: '9%'}
            , {field: 'userStatus', title: '状态', width: '5%',templet: function (d) {
                return d.userStatus == '01' ? "正常" : "停用";
            }}
            , {field: 'createTime', title: '创建时间', width: '12%',templet: function (d) {
                return beone.formatDate(d.createTime * 1000, 'yyyy-MM-dd HH:mm:ss');
            }}
            , {field: 'lastVisit', title: '最后登录时间', width: '12%',templet: function (d) {
                return beone.formatDate(d.lastVisit * 1000, 'yyyy-MM-dd HH:mm:ss');
            }}
            , {field: 'showRoles', title: '拥有角色', width: '15%'}
            , {field: 'userSex', title: '性别', width: '5%', templet: function (d) {
                return d.userSex == '01' ? "男" : "女";
            }}
            , {field: 'right', title: '操作', width: '22.8%', toolbar: "#barDemo"}
        ]]
        , page: true, request:{pageName:'page',limitName:'rows'}, height: 'full-200'
    });
    exports("user",{});
});